#include "apitwitter.h"

ApiTwitter::ApiTwitter()
{
}

QSqlQuery ApiTwitter::getUserTimeline(QString id)
{

    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "./database.db" );
    db.open();
    qDebug() << db.lastError();

    QSqlQuery query("SELECT * FROM timeline WHERE usuario_id='"+id+"' ORDER BY id DESC");
    qDebug() << query.lastError();
    db.close();

    return query;
}

bool ApiTwitter::newTweet(QString id, QString tweet )
{
    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "./database.db" );
    db.open();

    QDateTime data = QDateTime::currentDateTime();

    QSqlQuery query;
    query.prepare("INSERT INTO timeline (usuario_id, texto, data)"
                  "VALUES (:usuario_id, :texto, :data)");
    query.bindValue(":usuario_id", id.toInt());
    query.bindValue(":texto", tweet);
    query.bindValue(":data", data.toLocalTime().toString());
    query.exec();

    qDebug() << query.lastError();
    db.close();

    return true;
}
